package com.mycompany.app.hr.common.longrunningtask;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * A abstract LongRunningTask Base Class
 * 
 * @author kevin.wang
 */
public abstract class AbstractLongRunningTask implements ILongRunningTask {
    public static final Log log = LogFactory
            .getLog(AbstractLongRunningTask.class);

    long startTime = System.currentTimeMillis();

    protected ProgressMeter progress;

//    private ILockManager lockManager;
//
//    private Object lockToken;

    protected AbstractLongRunningTask() {

        progress = new ProgressMeter();
        progress.setStatus("Initializing... ");
    }

    public void run() {

        progress.setStatus("Starting... ");
        startTime = System.currentTimeMillis();
//        lockManager.acquireLock(lockToken);
        doRun();
//        lockManager.releaseLock(lockToken);
    }

    public abstract void doRun();

    public int getPercentageComplete() {

        return progress.getPercentageComplete();
    }

    public String getCurrentStatus() {

        return progress.getStatus();
    }

    public long getElapsedTime() {

        return System.currentTimeMillis() - startTime;
    }

    public long getEstimatedTimeRemaining() {

        long elapsedTime = getElapsedTime();

        if (getPercentageComplete() == 0)
            return 0;

        long totalTimeEstimate = 100 * elapsedTime / getPercentageComplete();
        return totalTimeEstimate - elapsedTime;
    }

    public boolean isComplete() {

        return getPercentageComplete() == 100;
    }
}
